package ogdkp.dialog;

import ogdkp.actions.AddLootActionInfo;
import ogdkp.om.Bank;
import ogdkp.om.CategoriesEnum;
import ogdkp.om.LootCache;
import ogdkp.om.LootItem;
import ogdkp.om.Player;

import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class AddLootDialog extends TitleAreaDialog {

	private static final String SELECTION_NONE = "(none)";

	private static final int THUNDERFORGED_COST = 0;

	private Combo nameText;

	private Text valueText;

	private String name;

	private String value;

	private Combo bossCombo;

	private Button normalButton;

	private Button heroicButton;

	private final Player player;

	private Button highBid;

	private Button lowBid;

	private Button offspecBid;

	private final AddLootActionInfo info;

	private Button thunderforgedCheck;

	public AddLootDialog(final Shell parentShell, final Player player, final AddLootActionInfo info) {
		super(parentShell);
		this.player = player;
		this.info = info;
	}

	@Override
	public void create() {
		super.create();
		setTitle("Add Loot");
		setMessage("The following information is required.");
	}

	@Override
	protected Control createDialogArea(final Composite parent) {
		final Composite container = new Composite(parent, SWT.NONE);

		final GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		layout.marginTop = 4;
		container.setLayout(layout);

		final GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		data.widthHint = 350;
		container.setLayoutData(data);

		new Label(container, SWT.NONE).setText("Difficulty:");

		final Composite typeComposite = new Composite(container, SWT.NONE);
		final GridLayout typeLayout = new GridLayout();
		typeLayout.numColumns = 3;
		typeComposite.setLayout(typeLayout);

		normalButton = new Button(typeComposite, SWT.RADIO);
		normalButton.setText("Normal");
		normalButton.setSelection(true);

		heroicButton = new Button(typeComposite, SWT.RADIO);
		heroicButton.setText("Heroic");

		thunderforgedCheck = new Button(typeComposite, SWT.CHECK);
		thunderforgedCheck.setText("Thunderforged");

		new Label(container, SWT.NONE).setText("Boss Filter");

		bossCombo = new Combo(container, SWT.READ_ONLY | SWT.BORDER);
		bossCombo.setItems(LootCache.getInstance().getBosses(null));
		bossCombo.add(SELECTION_NONE, 0);
		bossCombo.select(0);

		addGridData(bossCombo);
		bossCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				updateItemList();
			}
		});

		new Label(container, SWT.NONE).setText("Name:");

		nameText = new Combo(container, SWT.BORDER);
		nameText.setItems(LootCache.getInstance().getLootNamesFor(null));
		nameText.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				handleBid();
			}
		});

		addGridData(nameText);

		new Label(container, SWT.NONE).setText("Bid:");

		final Composite bidComposite = new Composite(container, SWT.NONE);
		final GridLayout bidLayout = new GridLayout();
		bidLayout.numColumns = 3;
		bidComposite.setLayout(bidLayout);

		highBid = new Button(bidComposite, SWT.RADIO);
		highBid.setText("High");
		highBid.setSelection(true);
		highBid.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				handleBid();
			}
		});

		lowBid = new Button(bidComposite, SWT.RADIO);
		lowBid.setText("Low");
		lowBid.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				handleBid();
			}
		});

		offspecBid = new Button(bidComposite, SWT.RADIO);
		offspecBid.setText("Offspec");
		offspecBid.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				handleBid();
			}
		});

		new Label(container, SWT.NONE).setText("Value:");

		valueText = new Text(container, SWT.BORDER);

		addGridData(valueText);

		if (player instanceof Bank) {
			valueText.setText("0");
		}

		if (name != null) {
			String displayName = name;
			if (displayName.startsWith("Heroic")) {
				displayName = displayName.substring("Heroic ".length());
				heroicButton.setSelection(true);
				normalButton.setSelection(false);
			}
			if (displayName.endsWith("(Thunderforged)")) {
				displayName = displayName.substring(0, displayName.length() - "(Thunderforged)".length() - 1);
				thunderforgedCheck.setSelection(true);
			} else {
				thunderforgedCheck.setSelection(false);
			}
			nameText.setText(displayName);
		}
		if (value != null) {
			valueText.setText(value);
		}

		if (info != null) {
			final String boss = info.getBoss();
			if ("Heroic".equals(info.getDifficulty())) {
				heroicButton.setSelection(true);
				normalButton.setSelection(false);
			}
			if (boss != null && boss.length() > 0) {
				for (int i = 0; i < bossCombo.getItemCount(); i++) {
					if (boss.equals(bossCombo.getItem(i))) {
						bossCombo.select(i);
						updateItemList();
						break;
					}
				}
			}
		}

		return container;
	}

	private void updateItemList() {
		if (bossCombo.getText().equals(SELECTION_NONE)) {
			nameText.setItems(LootCache.getInstance().getLootNamesFor(null));
		} else {
			nameText.setItems(LootCache.getInstance().getLootNamesFor(bossCombo.getText()));
		}

	}

	protected void handleBid() {
		if (player instanceof Bank == false) {
			if (highBid.getSelection()) {
				final LootItem item = LootCache.getInstance().getLootItemFor(nameText.getText());
				if (item != null) {
					valueText.setText(CategoriesEnum.getCostFor(item.getCategory()) + "");
				}
			} else if (lowBid.getSelection()) {
				valueText.setText("5");
			} else {
				valueText.setText("0");
			}
		}

	}

	private void addGridData(final Control composite) {
		final GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.grabExcessHorizontalSpace = true;
		composite.setLayoutData(gd);

	}

	@Override
	public void okPressed() {
		if (isValid()) {
			if (heroicButton.getSelection()) {
				this.name = "Heroic " + nameText.getText();
			} else {
				this.name = nameText.getText();
			}
			if (thunderforgedCheck.getSelection()) {
				this.name += " (Thunderforged)";
				this.value = Integer.parseInt(valueText.getText()) + THUNDERFORGED_COST + "";
			} else {
				this.value = valueText.getText();
			}
			super.okPressed();
		}

	}

	public boolean isValid() {
		try {
			Integer.parseInt(valueText.getText());
			return true;
		} catch (final NumberFormatException e) {
			setErrorMessage("Value must be a number.");
		}
		return false;
	}

	public String getName() {
		return name;
	}

	public String getValue() {
		return value;
	}

	public void setName(final String name) {
		this.name = name;

	}

	public void setValue(final String value) {
		this.value = value;
	}
}
